package com.huhu.contact.view;

import ohos.agp.components.*;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.utils.TextAlignment;
import ohos.app.Context;
import ohos.multimodalinput.event.MmiPoint;
import ohos.multimodalinput.event.TouchEvent;


public class LetterView extends DirectionalLayout implements Component.TouchEventListener {
    private CharacterClickListener mListener;

    public LetterView(Context context) {
        super(context);
    }


    public LetterView(Context context, AttrSet attrs) {
        super(context, attrs);


        setOrientation(VERTICAL);

        initView();
//        setEstimateSizeListener(this);
        if (!isClickable()) setClickable(true);
        setTouchEventListener(this);
//        addDrawTask(this);
        mListener = null;
    }

    // 分类
    private static final String[] ASSORT_TEXT = {"A", "B", "C", "D", "E", "F", "G",
            "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T",
            "U", "V", "W", "X", "Y", "Z", "#"};

    private void initView() {
//        addComponent(buildImageLayout());

//        for (char i = 'A'; i <= 'Z'; i++) {
        for (String character : ASSORT_TEXT) {
            Text tv = buildTextLayout(character);

            addComponent(tv);
        }

//        addComponent(buildTextLayout("#"));
    }

    private Text buildTextLayout(final String character) {
        LayoutConfig layoutConfig = new LayoutConfig(ComponentContainer.LayoutConfig.MATCH_PARENT, ComponentContainer.LayoutConfig.MATCH_CONTENT);
        layoutConfig.weight = 1;
        layoutConfig.alignment = LayoutAlignment.CENTER;
        Text tv = new Text(mContext);
        tv.setLayoutConfig(layoutConfig);
        tv.setTextAlignment(TextAlignment.CENTER);
        tv.setTextSize(AttrHelper.fp2px(14, mContext));
        tv.setClickable(true);

        tv.setText(character);

        tv.setClickedListener(component -> {
            if (mListener != null) {
                mListener.clickCharacter(character);
            }
        });
        return tv;
    }

    private int mSelectIndex = -1;
    @Override
    public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
        //判断是哪一个字母被点击了
        MmiPoint absPoint = touchEvent.getPointerPosition(touchEvent.getIndex());
        int nIndex = (int) (absPoint.getY() / getHeight() * ASSORT_TEXT.length);
        if (nIndex >= 0 && nIndex < ASSORT_TEXT.length) {
            switch (touchEvent.getAction()) {
                case TouchEvent.POINT_MOVE:
                    // 如果滑动改变
                    if (mSelectIndex != nIndex) {
                        mSelectIndex = nIndex;
                        showCharacter(ASSORT_TEXT[mSelectIndex]);
                        if (mListener != null) {
                            mListener.onTouchAssortListener(ASSORT_TEXT[mSelectIndex]);
                        }
                    }
                    break;
                case TouchEvent.PRIMARY_POINT_DOWN:
                    mSelectIndex = nIndex;
                    showCharacter(ASSORT_TEXT[mSelectIndex]);
                    if (mListener != null) {
                        mListener.onTouchAssortListener(ASSORT_TEXT[mSelectIndex]);
                    }

                    break;
                case TouchEvent.PRIMARY_POINT_UP:
                    disShowCharacter();
                    mSelectIndex = -1;
                    break;
            }
        } else {
            mSelectIndex = -1;
            disShowCharacter();
        }
        invalidate();
        return true;
    }

    private void disShowCharacter() {
//        if (mPopupWindow != null) {
//            mPopupWindow.dismiss();
//            mPopupWindow = null;
//        }
    }

    /**
     * 显示弹出的字符
     *
     * @param string
     */
    private void showCharacter(String string) {
//
//        if (mPopupWindow != null) {
//            text.setText(string);
//        } else {
//            mPopupWindow = new PopupWindow(layoutView, 100, 100, false);
//            mPopupWindow.showAtLocation(mAttachActivity.getWindow().getDecorView(), Gravity.CENTER, 0, 0);
//        }
//        text.setText(string);
    }

//    private Image buildImageLayout() {
//
//        Image iv = new Image(mContext);
//        iv.setWidth(ComponentContainer.LayoutConfig.MATCH_PARENT);
//        iv.setHeight(ComponentContainer.LayoutConfig.MATCH_PARENT);
//
//        iv.setBackgroundResource(R.mipmap.arrow);
//
//        iv.setOnClickListener(new OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if (mListener != null) {
//                    mListener.clickArrow();
//                }
//            }
//        });
//        return iv;
//    }

    public void setCharacterListener(CharacterClickListener listener) {
        mListener = listener;
    }

    public interface CharacterClickListener {
        void onTouchAssortListener(String character);

        void clickCharacter(String character);

        void clickArrow();
    }
}